﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Frogism.Core;
using Frogism.Domain;
using NUnit.Framework;

namespace Frogism.Tests {
  [TestFixture]
  public class BookManagerTest {
    [Test]
    public void NewBookManager() {
      IBookManager bm = ManagerService.GetBookManager(true);
    }
    [Test]
    public void CheckYearList() {
      IBookManager bm = ManagerService.GetBookManager(true);
      Collection<int> years = bm.YearList;
      Assert.IsNotNull(years);
      Assert.Less(0, years.Count());
      Assert.AreEqual(DateTime.Now.Year, years[0]);
    }
    [Test]
    public void GetPublicBooks() {
      IBookManager bm = ManagerService.GetBookManager(true);
      IEnumerable<IBook> books = bm.GetPublicReading();
      foreach (IBook b in books) {
        Assert.IsFalse(b.IsFinished, "book{" + b.Title + "} is Finished.");
        Assert.IsFalse(b.IsPrivate,"book{" + b.Title + "} is Private.");
      }
    }

    [Test]
    public void UpdateTitle() {
      IBookManager bm = ManagerService.GetBookManager(true);

      IBook book = bm.Get(2);
      string tempTitle = book.Title;
      book.Title = "XUnit Test Title";

      bm.Update(book);

      IBook book2 = bm.Get(2);
      Assert.AreEqual("XUnit Test Title", book2.Title);

      book2.Title = tempTitle;
      bm.Update(book2);


    }
    [Test]
    public void UpdateRead()
    {
      IBookManager bm = ManagerService.GetBookManager(true);

        IBook book = bm.Get(2);
        IRead read = book.Reads[book.Reads.Count-1];
        int tempPage = read.Page;
        read.Page = 9999;
        bm.UpdateRead(book, read);

        IBook book2 = bm.Get(2);
        IRead read2= book2.Reads[book2.Reads.Count - 1];
        Assert.AreEqual(9999,read2.Page);

        read.Page = tempPage;
        bm.UpdateRead(book, read);
    }
    [Test]
    public void AddRead()
    {
      IBookManager bm = ManagerService.GetBookManager(true);

      IBook book = bm.Get(2);
      IRead read = bm.GetOrCreateTodaysRead(book);
      read.Page = 9999;
      bm.UpdateRead(book,read);
             
      IBook book2 = bm.Get(2);
      IRead read2 = book2.Reads[book2.Reads.Count - 1];
      Assert.AreEqual(9999, read2.Page);

     
      bm.DeleteRead(read2);
    }
    [Test]
    public void DeleteRead() {
      IBookManager bm = ManagerService.GetBookManager(true);

      IBook book = bm.Get(2);
      IRead read = book.Reads[book.Reads.Count - 1];
      int tempPage = read.Page;
      read.Page = 9999;
      bm.UpdateRead(book, read);

      IBook book2 = bm.Get(2);
      IRead read2 = book2.Reads[book2.Reads.Count - 1];
      Assert.AreEqual(9999, read2.Page);

      read.Page = tempPage;
      bm.UpdateRead(book, read);
    }

    [Test]
    public void GetAllBooks() {
      IBookManager bm = ManagerService.GetBookManager(true);
      IEnumerable<IBook> books = bm.GetAll();
      Assert.IsNotNull(books);
    }
    [Test]
    public void GetFinishedBooks()
    {
        IBookManager bm = ManagerService.GetBookManager(false);
        IEnumerable<IBook> books = bm.GetFinished();
        Assert.IsNotNull(books);
        foreach (IBook b in books)
        {
            Assert.IsTrue(b.IsFinished);
        }
    }
    [Test]
    public void GetOneBook() {
      IBookManager bm = ManagerService.GetBookManager(true);
      IBook b = bm.Get(2);
      Assert.IsNotNull(b);
    }
    [Test]
    public void BookTitle() {
      IBookManager bm = ManagerService.GetBookManager(true);
      IBook b = bm.Get(2);
      Assert.IsNotNull(b.Title);
    }
    [Test]
    public void BookAuthor() {
      IBookManager bm = ManagerService.GetBookManager(true);
      IBook b = bm.Get(2);
      Assert.IsNotNull(b.Author);
    }
    [Test]
    public void BookTotalPages() {
      IBookManager bm = ManagerService.GetBookManager(true);
      IEnumerable<IBook> books = bm.GetAll();
      Assert.IsNotNull(books.First().TotalPages);
      Assert.Greater(books.First().TotalPages, 0);
    }
    [Test]
    public void Read() {
      IBookManager bm = ManagerService.GetBookManager(true);
      IEnumerable<IBook> books = bm.GetAll();
      Assert.IsNotNull(books.First().Reads);
    }
    [Test]
    public void TestReadNotEmpty() {
      IBookManager bm = ManagerService.GetBookManager(true);
      IBook book = bm.Get(2);
      Assert.Greater(book.Reads.Count,0);
    }
  [Test]
    public void CurrentPage() {
      IBook book = new Book();
      Read read = new Read();
      read.Page = 1;
      read.ReadDate = DateTime.Now;
      book.Reads.Add(read);
      
      read = new Read();
      read.Page = 99;
      read.ReadDate = DateTime.Now;
      book.Reads.Add(read);
      
      read = new Read();
      read.Page = 55;
      read.ReadDate = DateTime.Now;
      book.Reads.Add(read);

      Assert.AreEqual(99, book.TotalRead);
    }
    [Test]
    public void PagesToGo() {
      Book book = new Book();
      book.LastPageNumber = 100;
      Read read = new Read();
      read.Page = 1;
      read.ReadDate = DateTime.Now;
      book.Reads.Add(read);

      Read read2 = new Read();
      read2.Page = 88;
      read2.ReadDate = DateTime.Now;
      book.Reads.Add(read2);

      read = new Read();
      read.Page = 55;
      read.ReadDate = DateTime.Now;
      book.Reads.Add(read);

      Assert.AreEqual(book.TotalPages - read2.Page, book.ToGo);
    }
    [Test]
    public void TotalReadWithZeroReads() {
      IBook book = new Book();
      book.LastPageNumber = 101;

      Assert.AreEqual(0, book.TotalRead);
    }

    [Test]
    public void PercentRead() {
      Book book = new Book();
      book.LastPageNumber = 101;
      Read read = new Read();
      read.Page = 1;
      read.ReadDate = DateTime.Now;
      book.Reads.Add(read);

      Read read2 = new Read();
      read2.Page = 88;
      read2.ReadDate = DateTime.Now;
      book.Reads.Add(read2);

      read = new Read();
      read.Page = 55;
      read.ReadDate = DateTime.Now;
      book.Reads.Add(read);

      Assert.AreEqual(88.0 / 101.0 * 100.0, book.PercentRead);
    }
    
  }
}
